package com.platform.modules.app.controller;

import com.platform.common.utils.RestResponse;
import com.platform.modules.custmoer.dto.ExpenseParams;
import com.platform.modules.custmoer.entity.ExpenseEntity;
import com.platform.modules.custmoer.service.ExpenseService;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户报销接口
 */
@Slf4j
@RestController
@RequestMapping("/app/expense")
public class AppExpenseController {

    @Autowired
    private ExpenseService expenseService;

    /**
     * 报销查询
     * @param params
     * @return
     */
    @PostMapping("/query")
    public RestResponse query(@RequestBody Map params){
        Map<String, Object> map = new HashMap<>();
        if(params.get("startTime") != null && params.get("endTime") != null){
        }
        List<ExpenseEntity> list = expenseService.queryAll(params);
        map.put("msg","success");
        map.put("code",0);
        map.put("dataList",list);
        return RestResponse.success(map);
    }

    /**
     * 报销发起
     * @param expenseParams
     * @return
     */
    @PostMapping("/add")
    public RestResponse add(@RequestBody ExpenseParams expenseParams){
        Map<String, Object> map = new HashMap<>();

        ExpenseEntity expenseEntity = new ExpenseEntity();
        BeanUtils.copyProperties(expenseParams,expenseEntity);
        expenseEntity.setCreateTime(new Date());
        expenseEntity.setPrice(new BigDecimal(expenseParams.getPrice()));
        expenseEntity.setStatus(1);//1待审核
        expenseService.save(expenseEntity);
        map.put("msg","success");
        map.put("code",0);
        return RestResponse.success(map);
    }

    /**
     * 报销发起
     * @param expenseParams
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody ExpenseParams expenseParams){
        Map<String, Object> map = new HashMap<>();

        ExpenseEntity expenseEntity = new ExpenseEntity();
        BeanUtils.copyProperties(expenseParams,expenseEntity);
        expenseEntity.setCreateTime(new Date());
        expenseEntity.setPrice(new BigDecimal(expenseParams.getPrice()));
        expenseEntity.setStatus(1);//1待审核
        expenseService.update(expenseEntity);
        map.put("msg","success");
        map.put("code",0);
        return RestResponse.success(map);
    }

    @GetMapping("/detail/{id}")
    public RestResponse detail(@PathVariable String id){
        Map<String, Object> map = new HashMap<>();
        ExpenseEntity expenseEntity = expenseService.getById(id);
        //expenseEntity.setFileName(expenseEntity.getFileName());
        map.put("data",expenseEntity);
        return RestResponse.success(map);
    }

    /**
     * 更改报销状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/cancel/{id}/{status}")
    public RestResponse cancel(@PathVariable String id,@PathVariable String status){
        Map<String, Object> map = new HashMap<>();
        ExpenseEntity expenseEntity = expenseService.getById(id);
        expenseEntity.setStatus(Integer.parseInt(status));
        expenseService.update(expenseEntity);
        map.put("msg","success");
        map.put("code",0);
        return RestResponse.success(map);
    }
}
